package com.jsyh.maidu.activity.me;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.adapter.EvaluateListAdapter;
import com.jsyh.maidu.model.EvaluateModle;
import com.jsyh.maidu.presenter.EvaluateListPresenter;
import com.jsyh.maidu.views.EvaluateView;

public class EvalueteListActivity extends Activity implements EvaluateView,View.OnClickListener,AbsListView.OnScrollListener{

    ListView lv_evaluate;
    EvaluateListAdapter evaadapter;
    TextView tv_all,tv_wait,tv_finish,tv_title;
    EvaluateListPresenter evaluateListPresenter;
    ImageView iv_back;

    boolean scroll=false;

    int page=1;
    String type="all";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluete_list);

    }

    public void InitView()
    {
        lv_evaluate= (ListView) findViewById(R.id.lv_evalist);
        tv_all= (TextView) findViewById(R.id.tv_evalist_all);
        tv_finish= (TextView) findViewById(R.id.tv_evalist_finish);
        tv_wait= (TextView) findViewById(R.id.tv_evalist_wait);
        tv_title= (TextView) findViewById(R.id.title);
        iv_back= (ImageView) findViewById(R.id.back);

        evaadapter=new EvaluateListAdapter(this,R.layout.evaluate_item);
        lv_evaluate.setAdapter(evaadapter);
        tv_wait.setOnClickListener(this);
        tv_finish.setOnClickListener(this);
        tv_all.setOnClickListener(this);
        tv_title.setText("我的评价");
        iv_back.setOnClickListener(this);

         evaluateListPresenter=new EvaluateListPresenter(EvalueteListActivity.this,this);
        evaluateListPresenter.LoadEvaluateList("all",page);
        lv_evaluate.setOnScrollListener(this);
    }

    @Override
    public void getEvaluateList(EvaluateModle model) {
        if(model.getData()!=null){
            evaadapter.addAll(model.getData());
            scroll=true;
        }else
        {
            scroll=false;
            Toast.makeText(this,"没有更多订单啦", Toast.LENGTH_LONG).show();
        }



    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.tv_evalist_all:
                evaadapter.clear();
                type="all";
                page=1;
                scroll=false;
                scoll=false;
                evaluateListPresenter.LoadEvaluateList(type,page);
                setStatic(true,false,false);
                break;
            case R.id.tv_evalist_finish:
                evaadapter.clear();
                type="finish";
                page=1;
                scroll=false;
                scoll=false;
                evaluateListPresenter.LoadEvaluateList(type,page);
                setStatic(false,false,true);
                break;
            case R.id.tv_evalist_wait:
                evaadapter.clear();
                type="wait";
                page=1;
                scroll=false;
                scoll=false;
                evaluateListPresenter.LoadEvaluateList(type,page);
                setStatic(false,true,false);
                break;
            case R.id.back:
                finish();
                break;

        }

    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void setStatic(boolean all, boolean wait, boolean finish)
    {

        Drawable drawable1 = getResources().getDrawable(R.mipmap.pingjia1);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        Drawable drawable2 = getResources().getDrawable(R.mipmap.pingjia2);
        drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        if(all)
        {
            tv_all.setTextColor(getResources().getColor(R.color.white));
            tv_all.setBackgroundColor(getResources().getColor(R.color.red));
            tv_all.setCompoundDrawables(drawable2, null, null, null);
        }else {
            tv_all.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_all.setBackgroundColor(getResources().getColor(R.color.white));
            tv_all.setCompoundDrawables(drawable1, null, null, null);
        }
        if(wait)
        {
            tv_wait.setTextColor(getResources().getColor(R.color.white));
            tv_wait.setBackgroundColor(getResources().getColor(R.color.red));
            tv_wait.setCompoundDrawables(drawable2, null, null, null);
        }else {
            tv_wait.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_wait.setBackgroundColor(getResources().getColor(R.color.white));
            tv_wait.setCompoundDrawables(drawable1, null, null, null);
        }
        if(finish)
        {
            tv_finish.setTextColor(getResources().getColor(R.color.white));
            tv_finish.setBackgroundColor(getResources().getColor(R.color.red));
            tv_finish.setCompoundDrawables(drawable2, null, null, null);
        }else {
            tv_finish.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_finish.setBackgroundColor(getResources().getColor(R.color.white));
            tv_finish.setCompoundDrawables(drawable1, null, null, null);
        }
    }

    Boolean scoll=false;

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        scoll=true;


    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if ((firstVisibleItem+visibleItemCount)>=totalItemCount && scroll && scoll)
        {
            scroll=false;
            page++;
            evaluateListPresenter.LoadEvaluateList(type,page);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
       scroll=false;

        page=1;
        InitView();
    }
}
